home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  8.1 KB  |  341 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        menus.c
  4.  
  5. Purpose:    This module handles menu selections.
  6.  
  7.  
  8.  
  9. Dialectic -=- dialect text conversion extraordinare
  10. Copyright ©1994, Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "graphics.h"
  30. #include "menus.h"
  31. #include "help.h"
  32. #include "prefs.h"
  33. #include "environment.h"
  34. #include "program globals.h"
  35. #include "dialectic.h"
  36. #include "dialectic dispatch.h"
  37. #include "dialectic scrap.h"
  38.  
  39. MenuHandle        gAppleMenu;
  40. MenuHandle        gFileMenu;
  41. MenuHandle        gEditMenu;
  42. MenuHandle        gOptionsMenu;
  43. MenuHandle        gHelpMenu;
  44. MenuHandle        gDialectMenu;
  45.  
  46. enum
  47. {
  48.     helpMenu=200,
  49.     appleMenu = 400, fileMenu, editMenu, optionsMenu, dialectMenu,
  50.     
  51.     aboutItem = 1, aboutMSGItem, helpPointerItem,
  52.     openItem=1, closeItem, forceQuitItem = 4, quitItem,
  53.     undoItem = 1, cutItem = 3, copyItem, pasteItem, clearItem, showClipboardItem = 8,
  54.         convertClipboardItem,
  55.     showSaveItem = 1, addSuffixItem, showProgressItem, rtfItem
  56. };
  57.  
  58. /*-----------------------------------------------------------------------------------*/
  59. /* internal stuff for menus.c                                                        */
  60.  
  61. void HandleAppleMenu(int menuItem);
  62. void HandleFileMenu(int menuItem);
  63. void HandleEditMenu(int menuItem);
  64. void HandleViewMenu(int menuItem);
  65. void HandleOptionsMenu(int menuItem);
  66. void HandleDialectMenu(int menuItem);
  67. void HandleHelpMenu(int menuItem);
  68.  
  69.  
  70. Boolean InitTheMenus(void)
  71. {
  72.     Handle        MBARHandle;
  73.     
  74.     if ((MBARHandle=GetNewMBar(400))==0L)        /* sez which menus are in menu bar. */
  75.         return FALSE;
  76.     SetMenuBar(MBARHandle);                        /* set this to be THE menu bar to use. */
  77.     
  78.     if ((gAppleMenu=GetMHandle(appleMenu))==0L)    /* GetNewMBar also got menu handles of */
  79.         return FALSE;
  80.     if ((gFileMenu=GetMHandle(fileMenu))==0L)    /* every menu it includes, so just */
  81.         return FALSE;
  82.     if ((gEditMenu=GetMHandle(editMenu))==0L)    /* grab these handles and assign them */
  83.         return FALSE;
  84.     if ((gOptionsMenu=GetMHandle(optionsMenu))==0L)    /* to the appropriate menus */
  85.         return FALSE;
  86.     if ((gDialectMenu=GetMHandle(dialectMenu))==0L)    /* ...there, was that so hard? */
  87.         return FALSE;
  88.     if ((gHelpMenu = GetMenu(helpMenu))==0L)        /* help menu is not in menu bar, so */
  89.         return FALSE;
  90.     InsertMenu(gHelpMenu, -1);                    /* get it manually and insert it */
  91.     
  92.     AddResMenu(gAppleMenu, 'DRVR');                /* adds control panels to apple menu */
  93.     
  94.     AdjustMenus();                                /* dim/enable/check/mark menus/items */
  95.     DrawMenuBar();                                /* draws the actual menu bar */
  96.     
  97.     return TRUE;
  98. }
  99.  
  100. void AdjustMenus(void)
  101. {
  102.     WindowPeek        theWindow;
  103.     int                kind;
  104.     int                i;
  105.     unsigned char    iter;
  106.     unsigned long    dummy;
  107.     
  108.     if (gInProgress)
  109.     {
  110.         DisableItem(gAppleMenu, aboutItem);
  111.         DisableItem(gAppleMenu, aboutMSGItem);
  112.         DisableItem(gAppleMenu, helpPointerItem);
  113.         if (gSystemSevenOrLater)
  114.         {
  115.             DisableItem(gFileMenu, openItem);
  116.             DisableItem(gFileMenu, closeItem);
  117.             DisableItem(gFileMenu, quitItem);
  118.         }
  119.         else DisableItem(gFileMenu, 0);
  120.         DisableItem(gEditMenu, 0);
  121.         DisableItem(gOptionsMenu, 0);
  122.         DisableItem(gDialectMenu, 0);
  123.     }
  124.     else
  125.     {
  126.         EnableItem(gAppleMenu, aboutItem);
  127.         EnableItem(gAppleMenu, aboutMSGItem);
  128.         EnableItem(gAppleMenu, helpPointerItem);
  129.         if (gSystemSevenOrLater)
  130.         {
  131.             EnableItem(gFileMenu, openItem);
  132.             EnableItem(gFileMenu, closeItem);
  133.             EnableItem(gFileMenu, quitItem);
  134.         }
  135.         EnableItem(gFileMenu, 0);
  136.         EnableItem(gEditMenu, 0);
  137.         EnableItem(gOptionsMenu, 0);
  138.         EnableItem(gDialectMenu, 0);
  139.         
  140.         LoadScrap();
  141.         if (GetScrap(0L, 'TEXT', &dummy)==noTypeErr)
  142.         {
  143.             DisableItem(gEditMenu, showClipboardItem);
  144.             DisableItem(gEditMenu, convertClipboardItem);
  145.         }
  146.         else
  147.         {
  148.             EnableItem(gEditMenu, showClipboardItem);
  149.             EnableItem(gEditMenu, convertClipboardItem);
  150.         }
  151.         
  152.         theWindow = (WindowPeek)FrontWindow();
  153.         kind = theWindow ? theWindow->windowKind : 0;
  154.         
  155.         if (kind>=0)
  156.         {
  157.             DisableItem(gEditMenu, undoItem);
  158.             DisableItem(gEditMenu, cutItem);
  159.             DisableItem(gEditMenu, copyItem);
  160.             DisableItem(gEditMenu, pasteItem);
  161.             DisableItem(gEditMenu, clearItem);
  162.         }
  163.         else
  164.         {
  165.             EnableItem(gEditMenu, undoItem);
  166.             EnableItem(gEditMenu, cutItem);
  167.             EnableItem(gEditMenu, copyItem);
  168.             EnableItem(gEditMenu, pasteItem);
  169.             EnableItem(gEditMenu, clearItem);
  170.         }
  171.         
  172.         if (theWindow)
  173.             EnableItem(gFileMenu, closeItem);
  174.         else
  175.             DisableItem(gFileMenu, closeItem);
  176.     }
  177.     
  178.     if (gSystemSevenOrLater)
  179.         EnableItem(gFileMenu, forceQuitItem);
  180.     else
  181.         DisableItem(gFileMenu, forceQuitItem);
  182.     
  183.     CheckItem(gOptionsMenu, showSaveItem, gShowSaveDialog);
  184.     CheckItem(gOptionsMenu, addSuffixItem, gAddSuffix);
  185.     CheckItem(gOptionsMenu, showProgressItem, gShowProgress);
  186.     CheckItem(gOptionsMenu, rtfItem, gUseRTF);
  187.     SetSuffixMenuItem(gOptionsMenu, addSuffixItem);
  188.     for (iter=0; iter<CountMItems(gDialectMenu); iter++)
  189.         CheckItem(gDialectMenu, iter+1, (iter==gWhichDialect));
  190.     HiliteMenu(0);
  191. }
  192.  
  193. void HandleMenu(long mSelect)
  194. {
  195.     int            menuID = HiWord(mSelect);
  196.     int            menuItem = LoWord(mSelect);
  197.     
  198.     switch (menuID)
  199.     {
  200.         case appleMenu:
  201.             HandleAppleMenu(menuItem);
  202.             break;
  203.         case fileMenu:
  204.             HandleFileMenu(menuItem);
  205.             break;    
  206.         case editMenu:
  207.             HandleEditMenu(menuItem);
  208.             break;
  209.         case optionsMenu:
  210.             HandleOptionsMenu(menuItem);
  211.             break;
  212.         case helpMenu:
  213.             HandleHelpMenu(menuItem);
  214.             break;
  215.         case dialectMenu:
  216.             HandleDialectMenu(menuItem);
  217.             break;
  218.       }
  219. }
  220.  
  221. void DoTheCloseThing(WindowPeek theWindow)
  222. /* a standard close procedure, called when "close" is chosen from File menu and when
  223.    a window is closed through its close box */
  224. {
  225.     Boolean            gotone;
  226.     int                i;
  227.     int                kind;
  228.     
  229.     kind = theWindow ? theWindow->windowKind : 0;
  230.     if (kind<0)        /* DA window or other system window */
  231.         CloseDeskAcc(kind);
  232.     else
  233.     {
  234.         gotone=FALSE;
  235.         /* see if it's one of ours */
  236.         for (i=0; (i<NUM_WINDOWS) && (!gotone); i++)
  237.             gotone=(theWindow==gTheWindow[i]);
  238.         
  239.         if (gotone)        /* if it's one of ours...  see graphics.c */
  240.             CloseTheWindow(gTheWindowData[i-1]);    /* this may return FALSE = not closed */
  241.         else
  242.             DisposeWindow(theWindow);        /* not one of ours, so just close it */
  243.     
  244.         AdjustMenus();    /* may affect which menu items or menus are available, etc */
  245.     }
  246. }
  247.  
  248. void HandleAppleMenu(int menuItem)
  249. {
  250.     GrafPtr        savePort;
  251.     Str255        name;
  252.     
  253.     if (menuItem == aboutItem)
  254.         OpenTheWindow(kAbout);
  255.     if (menuItem == aboutMSGItem)
  256.         OpenTheWindow(kAboutMSG);
  257.     else if (menuItem > helpPointerItem+1)
  258.     {
  259.         GetPort(&savePort);
  260.         GetItem(gAppleMenu, menuItem, name);
  261.         OpenDeskAcc(name);
  262.         SetPort(savePort);
  263.     }
  264. }
  265.  
  266. void HandleFileMenu(int menuItem)
  267. {
  268.     WindowPtr            theWindow;
  269.     int                    i;
  270.     Boolean                gotone;
  271.     
  272.     switch (menuItem)
  273.     {
  274.         case openItem:
  275.             NewConvert();
  276.             break;
  277.         case closeItem:
  278.             DoTheCloseThing(FrontWindow());
  279.             break;
  280.         case forceQuitItem:
  281.             SysError(0x4e22);
  282.             break;
  283.         case quitItem:
  284.             gDone = TRUE;
  285.             break;
  286.     }
  287. }
  288.  
  289. void HandleEditMenu(int menuItem)
  290. {
  291.     switch (menuItem)
  292.     {
  293.         case showClipboardItem:
  294.             OpenTheWindow(kClipboard);
  295.             break;
  296.         case convertClipboardItem:
  297.             NewScrapConvert();
  298.             break;
  299.         default:
  300.             SystemEdit(menuItem-1);
  301.             break;
  302.     }
  303. }
  304.  
  305. void HandleHelpMenu(int menuItem)
  306. {
  307.     gLastHelp=gWhichHelp;
  308.     gWhichHelp=menuItem;
  309.     OpenTheWindow(kHelp);
  310. }
  311.  
  312. void HandleOptionsMenu(int menuItem)
  313. {
  314.     switch (menuItem)
  315.     {
  316.         case showSaveItem:
  317.             gShowSaveDialog=!gShowSaveDialog;
  318.             SaveThePrefs();
  319.             break;
  320.         case addSuffixItem:
  321.             gAddSuffix=!gAddSuffix;
  322.             SaveThePrefs();
  323.             break;
  324.         case showProgressItem:
  325.             gShowProgress=!gShowProgress;
  326.             SaveThePrefs();
  327.             break;
  328.         case rtfItem:
  329.             gUseRTF=!gUseRTF;
  330.             SaveThePrefs();
  331.             break;
  332.     }
  333. }
  334.  
  335. void HandleDialectMenu(int menuItem)
  336. {
  337.     gWhichDialect=menuItem-1;
  338.     SaveThePrefs();
  339.     AdjustMenus();
  340. }
  341.